package cn.edu.hrbcu.es.invigilatemanagerment.controller;

import cn.edu.hrbcu.es.invigilatemanagerment.pojo.Permission;
import cn.edu.hrbcu.es.invigilatemanagerment.pojo.RolePermission;
import cn.edu.hrbcu.es.invigilatemanagerment.service.IPermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

@Controller
@RequestMapping("/Permission")
public class PermissionController {
    @Autowired
    private IPermissionService permissionService;

    @RequestMapping("queryAllPermission")
    public String queryAllPermission(Model model,@RequestParam(value ="pageNow",defaultValue = "1")int pageNow,
                                     @RequestParam(value = "pageSize",defaultValue = "5")int pageSize){
        int totalPage = permissionService.queryPermissionCount();
        int pageCount = (int)Math.ceil((double)totalPage/pageSize);
        List<Permission> permissionsList = permissionService.queryPermissionsByPage(pageNow,pageSize);
        model.addAttribute("permissionsList",permissionsList);
        model.addAttribute("pageCount",pageCount);
        model.addAttribute("pageNow",pageNow);
        model.addAttribute("pageSize",pageSize);
        return "permissions";
    }

    @RequestMapping("toAddPermission")
    public String toAddRolePermission() {
        return "toaddpermission";
    }

    @RequestMapping("toUpdatePermission")
    public ModelAndView toUpdatePermission(Integer id)
    {
        Permission permission = permissionService.queryPermissionById(id);
        ModelAndView model = new ModelAndView("tpupdatepermission");
        model.addObject("permission",permission);
        return model;
    }
    @RequestMapping("updatePermissionById")
    @ResponseBody
    public String updatePermissionById(Permission permission)
    {
        permissionService.updatePermissionByPrimaryId(permission);
        return "OK";
    }

    @RequestMapping("insertPermission")
    @ResponseBody
    public String insertPermission(Permission permission)
    {
        permissionService.insertPermission(permission);
        return "OK";
    }

    @RequestMapping("deletePermissionById")
    @ResponseBody
    public String deletePermissionById(Integer id)
    {
        permissionService.deletePermissionByPrimaryId(id);
        return "OK";
    }

//    @RequestMapping("testselectPermissionsByTeacherId")
//    @ResponseBody
//    public List<Permission> testselectPermissionsByTeacherId(Integer id)
//    {
//        return permissionService.selectPermissionsByTeacherId(id);
//    }
}
